//
//  head.swift
//  KLM
//
//  Created by 朱雨 on 2021/6/2.
//

import UIKit

//第三库
import SnapKit
import SnapKitExtend
import RxSwift
import RxCocoa

@objcMembers
class KLMLocalizable: NSObject {
    //国际化
    static func LANGLOC(_ key : String) -> String {
        return NSLocalizedString(key, comment: "")
    }
}

var AppleStoreID: String {
    
    switch apptype {
    case .targetGN:
        return ""
    }
}
///buglyID
var BuglyAppID: String {
    switch apptype {
    case .targetGN:
        return ""
    }
}

//分多个包
enum AppType {
    case targetGN //国内版
}

var apptype: AppType {
    
#if target_GN
    return AppType.targetGN
#endif
}

/*** 常用 ***/
let KLMScreenW = UIScreen.main.bounds.size.width
let KLMScreenH = UIScreen.main.bounds.size.height
let KLMBounds = UIScreen.main.bounds

//导航栏等高度
let KLM_StatusBarHeight = UIApplication.shared.statusBarFrame.size.height //状态栏高度
let KLM_NavBarHeight = 44.0 //导航栏高度
let KLM_TopHeight = KLM_StatusBarHeight + CGFloat(KLM_NavBarHeight) //整个导航栏高度
let KLM_TabbarHeight = (KLM_StatusBarHeight>20 ? CGFloat(83) : CGFloat(49)) //底部tabbar高度
let KLM_BottomSafeAreaHeight = (KLM_StatusBarHeight>20 ? 34 : 0)

//keyWindow
let KLMKeyWindow = UIApplication.shared.keyWindow
    
//打印
func KLMLog<T>(_ parameter : T, file : String = #file, lineNumber : Int = #line)
{
    #if DEBUG
    let fileName = (file as NSString).lastPathComponent
    let time = KLMLogManager.sharedInstacnce.logDateTime()
    if let dic: Dictionary = parameter as? Dictionary<String, Any> {
        let message: String = dic.jsonPrint()
        print("[\(time) \(fileName):line:\(lineNumber)]\n --\(message)\n")
        return
    }
    
    print("[\(time) \(fileName):line:\(lineNumber)]\n --\(parameter)\n")
    #else
    
    #endif
}
/*** 颜色***/
func rgba(_ r : CGFloat, _ g : CGFloat, _ b : CGFloat, _ a : CGFloat) -> UIColor {
    return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}

func rgb(_ r : CGFloat, _ g : CGFloat, _ b : CGFloat) -> UIColor {
    return rgba(r, g, b, 1.0)
}

//APP导航栏颜色
var navigationBarColor: UIColor {
    return #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
///APP主题颜色
var appMainThemeColor: UIColor {
    return rgba(19, 207, 128, 1)
}

//APP背景颜色 - 不同页面
var appBackGroupColor: UIColor {
    return #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
}

//版本信息
let KLM_SYSTEM_VERSION = Float(UIDevice.current.systemVersion)
let KLM_APP_NAME: String = Bundle.main.infoDictionary?["CFBundleDisplayName"] as! String
let KLM_APP_VERSION: String = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
let KLM_APP_BUILD = Bundle.main.infoDictionary?["CFBundleVersion"] as! String

/*** UserDefaults ***/
func KLMSetUserDefault(_ key : String, _ value : Any?) -> Void {
    
    UserDefaults.standard.setValue(value, forKey: key)
    UserDefaults.standard.synchronize()
   
}

func KLMGetUserDefault(_ key : String) -> Any? {
    
    return UserDefaults.standard.value(forKey: key)
}

//rxswift
let disposeBag = DisposeBag()

//error HUD
func KLMShowError(_ error: MessageError?) {
    
    SVProgressHUD.showInfo(withStatus: error?.message)
    SVProgressHUD.dismiss(withDelay: 3)
}

/// 显示http请求错误
/// - Parameter error: NSError
func KLMHttpShowError(_ error: NSError) {
    SVProgressHUD.dismiss()
    if let message: String = error.userInfo["error"] as? String {
        SVProgressHUD.showInfo(withStatus: message)
        SVProgressHUD.dismiss(withDelay: 2)
    }
}

///
var baseUrl: String {

    return serverType.rawValue
    
}

enum ServerType: String {
    case Amazon = "https://ai.kaiwaresz.com/" //亚马逊
    case Alibaba = "https://iottest.kaiwaresz.com/" //阿里云
//    case Alibaba = "https://light.kaiwaresz.com/" //阿里云
//    case test = "http://iothub.kaiwaresz.com/" //测试使用
}

///服务器类型
var serverType: ServerType {
#if DEBUG
    switch apptype {
    case .targetGN:
        return ServerType.Alibaba
        
    default:
        return ServerType.Amazon
    }
#else
    switch apptype {
    case .targetGN:
        return ServerType.Alibaba
    default:
        return ServerType.Amazon
    }
#endif
}

///请求头加上这个，区分不同的APP
var tenantCode: String {
    var code = ""
    switch apptype {
    case .targetGN:
        code = "CN2502260"
    }
    return code
}

var appCode: Int {
    var code = 3
    switch apptype {
    case .targetGN:
        code = 8
    }
    return code
}
func KLMUrl(_ url: String) -> String {
 
    return baseUrl + url
}

